package com.chefu.operator.controller;

import com.chefu.common.pojo.PageResult;
import com.chefu.operator.pojo.ArticleCategory;
import com.chefu.operator.pojo.OperatorArticle;
import com.chefu.operator.service.ArticleService;
import com.chefu.operator.vo.ArticleListVo;
import com.chefu.operator.vo.ArticleVo;
import com.chefu.operator.vo.QueryArticlesRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
@Slf4j
public class ArticleControlelr {
    @Autowired(required = false)
    private ArticleService articleService;

    /**
     * 查询文章列表
     */
    @GetMapping("/articleList")
    public ResponseEntity<PageResult<ArticleListVo>> queryArticlesByPage(
            @RequestBody QueryArticlesRequest queryArticlesRequest
    ) {
        log.info("进入查询文章列表方法...");
        PageResult<ArticleListVo> articleList = articleService.queryArticlesByPage(queryArticlesRequest);
        return ResponseEntity.ok(articleList);
    }


    /**
     * 新增文章
     * 点击发布按钮异步请求新增文章，没有异常的情况下前端
     * 弹窗提示成功后跳转回文章列表页面
     */
    @PostMapping
    public ResponseEntity<String> addArticle(@RequestBody OperatorArticle operatorArticle) {
        log.info("进入新增文章方法...");
        String result = articleService.addArticle(operatorArticle);
        return ResponseEntity.ok(result);
    }


    /**
     * 修改文章
     * 首先走查询文章列表控制器，回显数据。
     * 点击修改按钮将数据更新到数据库。
     * 前端提示修改成功后跳转回文章列表页面。
     */
    @PutMapping
    public ResponseEntity<String> updateArticle(@RequestBody OperatorArticle operatorArticle) {
        log.info("进入修改文章方法...");
        String result = articleService.updateArticle(operatorArticle);
        return ResponseEntity.ok(result);
    }


    /**
     * 查看文章详情
     */
    @PutMapping("/{id}")
    public ResponseEntity<ArticleVo> queryArticle(@PathVariable("id") Long id) {
        log.info("进入查询文章详情方法");
        ArticleVo articleVo = articleService.queryArticle(id);
        return ResponseEntity.ok(articleVo);
    }

    /**
     * 修改文章发布状态
     */
    @PutMapping("/updateStatus")
    public ResponseEntity<Void> updateStatus(
            @RequestParam("id") Long id,
            @RequestParam("saleable") Boolean saleable) {
        log.info("进入修改文章状态方法");
        articleService.upadateStatus(id, saleable);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 查询文章分类列表
     */
    @GetMapping("/categoryList")
    public ResponseEntity<PageResult<ArticleCategory>> queryCategoryByPage(
            @RequestParam(value = "page", defaultValue = "1") Long page,
            @RequestParam(value = "rows", defaultValue = "15") Integer rows
    ) {
        log.info("进入查询文章分类列表的方法");
        PageResult<ArticleCategory> pageResult = articleService.queryCategoryByPage(page, rows);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 新增文章分类
     * 捕抓到异常弹窗提示
     * 没有捕抓到异常弹窗提示成功，并跳转回查询文章分类列表页面
     */
    @PostMapping("/addCategory")
    public ResponseEntity<String> addArticleCategory(
            @RequestBody ArticleCategory articleCategory
    ) {
        log.info("进入新增文章分类方法");
        String result = articleService.addArticleCategory(articleCategory);
        return ResponseEntity.ok(result);
    }

    /**
     * 修改文章分类
     */
    @PutMapping("/updateCategory")
    public ResponseEntity<String> updateCategoyr(
            @RequestBody ArticleCategory articleCategory
    ){
        log.info("进入修改文章分类方法");
        String result = articleService.updateCategory(articleCategory);
        return ResponseEntity.ok(result);
    }

    /**
     * 删除文章分类
     */
    @DeleteMapping("/delCategory/{id}")
    public ResponseEntity<String> delCategory(
            @PathVariable("id") Long id
    ){
        log.info("进入删除文章分类的方法");
        String result = articleService.delCategory(id);
        return ResponseEntity.ok(result);
    }
}
